<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<style type="text/css">
    .w100 {width: 85px; padding:5px 8px; border:1px solid #CCC; border-radius: 5px;}
    label {font-weight: normal;}
    .fixed-btn {position:fixed; right:35%;bottom:1%;}
    .price1 {width:70%;margin-bottom:5px;float:left;}
    .fa {font-size: 16px;}
</style>
<?=  yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>
<div style="position:relative;">

<?php $form = ActiveForm::begin([
                                 'id' => 'template-room-form',
                                 'action' => '/product/product-room/save',
                            ]); ?>

<input type="hidden" name="ProductRoom[product_id]" value="<?= $product->id ?>" >
<div class="product-room-form form-inline table-responsive">
    <h2 class="page-header"><small><?= $product->name ?> &nbsp; <?= $product->go_time ?></small>
		 <p class="pull-right">

         <a class="btn btn-danger" href="/product/product/view?id=<?=$product->id?>">返回详情</a>

         <a data-toggle="modal" data-target=".addroom-modal"  href="/product/product-room/create-room?product_id=<?= $product->id?>" class="btn btn-success">添加新舱房</a>
         <!-- a class="btn btn-primary" href="/product/product-room/create?product_id=</?=$product->id?>&type=2">自定义价格格式</a -->
		 </p>
    </h2>
	<div class="pull-right">
		<a class="btn btn-sm btn-warning" href="/product/product-room/create?product_id=<?=$product->id ?>&cate=2">全部舱房</a>
		<a class="btn btn-sm btn-info" href="/product/product-room/create?product_id=<?=$product->id ?>&cate=1">使用全部</a>
	</div>
    <table class="table table-condensed">
    <tr>
        <th>类型</th>
        <th>舱房名称</th>
        <th>人数</th>
        <th>所在楼层</th>
        <th>剩余</th>
        <th>库存数</th>
        <th>价格设置 <button class="btn btn-primary btn-sm btn-commiss" type="button" style="margin-left: 150px;" >后返</button> </th>
    </tr>
    <?php foreach($models as $key=>$model){ ?>
    <input type="hidden" name="ProductRoom[id][<?= $model->id ?>]" value="<?= $model->id ?>" >
	<?php if($cate == 1){ ?>
    <tr style="<?= ($model->show==0) ? 'display:none;' : ''  ?>" >
	<?php }else{ ?>
    <tr>
	<?php } ?>
        <td class="col-md-1">
            <?= !empty($model->room->type) ? Yii::$app->params['room_type'][$model->room->type] : '(未设置)' ?>
        </td>
        <td class="col-md-2"><?= $model->room->name?> <?= $model->room->code?></td>
        <td class="col-md-1"><?= $model->people ?>&nbsp;人</td>
        <td class="col-md-1">
            <?php echo yii::$app->ceils->getCeils($model->room->ceil); ?>
        </td>
        
        <td class="col-md-1">
			<label><?= $model->num - $model->stockNum ?></label>
        </td>
        <td class="col-md-1">
            <p>
			<input type="text" id="room-num" class="w100 stock" name="ProductRoom[num][<?=$model->id ?>]" value="<?= $model->num ?>" placeholder="库存数" data-id=<?= $model->id ?> >
			<input type="hidden" value="<?= $model->stockNum ?>" class="sell">
			</p>
        </td>
        <td class="col-md-6" style="position:relative;">
            <div class='price1'>
            	<input type="text" id="room-price" class="w100" name="ProductRoom[price][<?=$model->id?>]"  value="<?= $model->price?>" placeholder="价格">

            	<input type="text" id="room-inside_price" class="w100" name="ProductRoom[inside_price][<?=$model->id?>]"  value="<?= $model->inside_price ?>" placeholder="同业价格"> &nbsp;&nbsp;

            	<input type="text" id="room-commiss_price" class="w100 <?= ($model->show==1) ? 'commiss-val' : ''  ?> " name="ProductRoom[commiss_price][<?=$model->id?>]"  value="<?= $model->commiss_price ?>" placeholder="后返">
            </div>

            <div style="width:15%;float:left;" >
                <input class="magic-checkbox check-show" <?= ($model->show==1) ? 'checked' : ''  ?> type="checkbox" id="productroom-change-<?=($key+1)?>" name="ProductRoom[show][<?= $model->id ?>]" data-id=<?= $model->id ?> > 
                <label for="productroom-change-<?= ($key+1)?>">显示</label>
            </div>
            <div style="width:15%;float:left;" >
                <input class="magic-checkbox sell-out" <?= ($model->sell_out==1) ? 'checked' : ''  ?> type="checkbox" id="productroom-sellout-<?=($key+1)?>" name="ProductRoom[sell_out][<?= $model->id ?>]" data-id=<?= $model->id ?> > 
                <label for="productroom-sellout-<?= ($key+1)?>">售罄</label>
            </div>
        </td>
    </tr>
    <?php } ?>
    </table>
        
</div>
<div class="form-group">
    <label class="label" style="color:red;">房间价格说明 :</label>
    <textarea name="Product[room_remark]" rows=4 class="form-control"><?= $product->room_remark ?></textarea> 
</div>
<hr>
<?= Html::submitButton(Yii::t('app', '保 存'), ['class' => 'btn btn-primary col-md-3 fixed-btn']) ?>
<?php ActiveForm::end(); ?>

</div>

<div class="modal fade addroom-modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document">
      <div class="modal-content">
            <!-- 嵌入内容 --> 
      </div>
  </div>
</div>

<?php app\components\JsBlock::begin() ?>
<script>

	// 佣金后返全部赋值 
	$(".btn-commiss").click(function(){
		var price = $(".commiss-val").first().val();
		
		$(".commiss-val").each(function(k,v){
				
			console.log($(v).val(price));
		});
	});


    $(".check-show").click(function(){

		var t = $(this);
        var checked = $(this).is(':checked');   // 是否选中
		var room_id = $(this).data("id");

		console.log(room_id);

		if(checked == false){ // 取消显示
			// 检查该库存是否已经使用过	
        	$.get("/product/product-room/is-order-use", {id:room_id}, function(data){
				if(data == 'false'){
					alert('订单中已经使用无法隐藏');	
					t.prop("checked", true);
					console.log(data);
				}
        	});
		}
	});

    //保存是否售罄
    $(".sell-out").click(function(){
        var t = $(this);
        var checked = $(this).is(':checked');   // 是否选中
        var room_id = $(this).data("id");
      
        $.get("/product/product-room/sell-out", {id:room_id,checked:checked}, function(data){
             console.log(data);
        });
        
    });


	// 修改库存判断价格
    $(".stock").change(function(){//{{{
		var t = $(this);

		var num = $(this).val();
		var room_id = $(this).data("id");

		// 检查该库存修改后的数量是否小于已售卖的数量
        $.get("/product/product-room/check-sell", {id:room_id, num:num}, function(data){
			if(data != 'true'){
				alert('修改后的舱房数量不能小于已售出的数量');	
				t.val(data);
			}
        });
	})//}}}


	
</script>
<?php app\components\JsBlock::end() ?>
